from feapder import UpdateItem
from feapder.utils import tools
from datetime import datetime


class AmazonCategoryItem(UpdateItem):

    # 指定去重的key为 title、url，最后的指纹为title与url值联合计算的md5
    __unique_key__ = ["cate_father_cid",
                      "cate_name", "cate_level", "cate_source"]

    def __init__(self, *args, **kwargs):
        self.cid = None
        self.cate_name = None
        self.cate_level = None
        self.cate_url = None
        self.cate_father_cid = None
        self.cate_source = None
        self.is_floor = None

    def pre_to_db(self):
        """
        入库前的处理
        """
        self.cid = self.gen_id()

    def gen_id(self):
        return tools.get_md5(f'{self.cate_father_cid}{self.cate_name}{self.cate_level}{self.cate_source}')


class AmazonCategoryAsinsItem(UpdateItem):

    __unique_key__ = ["aid"]  # 指定去重的key为 title、url，最后的指纹为title与url值联合计算的md5

    def __init__(self, *args, **kwargs):
        # self.id = None
        self.cid = None
        self.asin = None
        self.url = None
        self.rank = None
        self.title = None
        self.price = None
        self.rate_num = None
        self.score = None
        self.cover = None
        self.sold_rank_text = None
        self.aid = self.gen_id()
        self.cid = None

    def pre_to_db(self):
        """
        入库前的处理
        """
        data = self.__dict__
        self.update_time = datetime.now()
        # data.update("table_name":slef.)

    def gen_id(self):
        return tools.get_md5(f'{self.cid}{self.asin}')
